package com.example.zzb.mycalculater;

import android.graphics.Color;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ScrollingTabContainerView;
import android.text.method.ScrollingMovementMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button button_1;
    Button button_2;
    Button button_3;
    Button button_4;
    Button button_5;
    Button button_6;
    Button button_7;
    Button button_8;
    Button button_9;
    Button button_0;
    Button button_point;
    Button button_equal;
    TextView all_result;
    TextView all_looking;
    String str;
    String all_str;
    boolean clear_flag = true;
    boolean clear_flag2 = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_1 = (Button) findViewById(R.id.button1);
        button_2 = (Button) findViewById(R.id.button2);
        button_3 = (Button) findViewById(R.id.button3);
        button_4 = (Button) findViewById(R.id.button4);
        button_5 = (Button) findViewById(R.id.button5);
        button_6 = (Button) findViewById(R.id.button6);
        button_7 = (Button) findViewById(R.id.button7);
        button_8 = (Button) findViewById(R.id.button8);
        button_9 = (Button) findViewById(R.id.button9);
        button_0 = (Button) findViewById(R.id.button0);
        button_point = (Button) findViewById(R.id.buttonpoint);
        button_equal = (Button) findViewById(R.id.buttonequal); //实例化按钮
        all_result = (TextView) findViewById(R.id.result);  //实例化屏幕
        all_looking = (TextView) findViewById(R.id.looking);
        all_looking.setMovementMethod(ScrollingMovementMethod.getInstance());

        button_0.setOnClickListener(this);
        button_1.setOnClickListener(this);
        button_2.setOnClickListener(this);
        button_3.setOnClickListener(this);
        button_4.setOnClickListener(this);
        button_5.setOnClickListener(this);
        button_6.setOnClickListener(this);
        button_7.setOnClickListener(this);
        button_8.setOnClickListener(this);
        button_9.setOnClickListener(this);
        button_point.setOnClickListener(this);
        button_equal.setOnClickListener(this); //设置点击事件

       button_5.setOnTouchListener(new View.OnTouchListener() {
            float X1 = 0;
            float Y1 = 0;
            float X2 = 0;
            float Y2 = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(!clear_flag2){
                    all_looking.setText("");
                    clear_flag2 = true;
                }
                str = all_result.getText().toString();
                all_str = all_looking.getText().toString();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        X1 = event.getX();
                        Y1 = event.getY();
                        X2 = event.getX();
                        Y2 = event.getY();
                        button_5.setBackgroundColor(Color.parseColor("#3ae0e2"));
                        break;
                    case MotionEvent.ACTION_MOVE:
                        X2 = event.getX();
                        Y2 = event.getY();
                        if (Y2 - Y1 > 50) {
                            button_5.setText("-");
                        } else if (Y1 - Y2 > 50) {
                            button_5.setText("+");
                        } else if (X2 - X1 > 50) {
                            button_5.setText("*");
                        } else if (X1 - X2 > 50) {
                            button_5.setText("/");
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        button_5.setBackgroundColor(Color.parseColor("#000000"));
                        if (Y2 - Y1 > 50) {
                            //向下滑動
                            all_str = all_str+str;
                            all_looking.setText(all_str + ((Button) v).getText());
                            str = "0";
                            all_result.setText("0");
                            clear_flag = true;
                            button_5.setText("5");
                        } else if (Y1 - Y2 > 50) {
                            //向上滑动
                            all_str = all_str+str;
                            all_looking.setText(all_str + ((Button) v).getText());
                            str = "0";
                            all_result.setText("0");
                            clear_flag = true;
                            button_5.setText("5");
                        } else if (X2 - X1 > 50) {
                            //向右滑动
                            all_str = all_str+str;
                            all_looking.setText(all_str + ((Button) v).getText());
                            str = "0";
                            all_result.setText("0");
                            clear_flag = true;
                            button_5.setText("5");
                        } else if (X1 - X2 > 50) {
                            //向左滑动
                            all_str = all_str+str;
                            all_looking.setText(all_str + ((Button) v).getText());
                            str = "0";
                            all_result.setText("0");
                            clear_flag = true;
                            button_5.setText("5");
                        } else {
                            if (clear_flag) {
                                clear_flag = false;
                                str = "";
                                all_result.setText("");
                            }
                            all_result.setText(str + ((Button) v).getText());
                        }

                        break;
                }
                return true;
            }
        });  //设置滑动监听事件
        all_result.setOnTouchListener(new View.OnTouchListener() {
            float X1 = 0;
            float X2 = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                str = all_result.getText().toString();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        all_result.setBackgroundColor(Color.parseColor("#9da4a4"));
                        X1 = event.getX();
                        X2 = event.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        X2 = event.getX();

                        break;
                    case MotionEvent.ACTION_UP:
                        all_result.setBackgroundColor(Color.parseColor("#ffffff"));
                        if (X2 - X1 > 50) {
                            //向右滑動
                            str = "0";
                            all_result.setText("0");
                                clear_flag = true;
                            all_str = "";
                            all_looking.setText("");
                        } else {
                            if (str.length() == 1){
                                str = "0";
                                all_result.setText("0");
                                clear_flag = true;
                            }else {
                                all_result.setText(str.substring(0,str.length()-1));
                                clear_flag = true;
                            }

                        }
                        break;
                }
                return true;
            }
        });

    }

    @Override
    public void onClick(View v) {
        if(!clear_flag2){
            all_looking.setText("");
            clear_flag2 = true;
        }
        str = all_result.getText().toString();
        switch (v.getId()) {
            case R.id.button0:
            case R.id.button1:
            case R.id.button2:
            case R.id.button3:
            case R.id.button4:
            case R.id.button6:
            case R.id.button7:
            case R.id.button8:
            case R.id.button9:
            case R.id.buttonpoint:
                if (clear_flag) {
                    clear_flag = false;
                    str = "";
                    all_result.setText("");
                }
                all_result.setText(str + ((Button) v).getText());
                break;
            case R.id.buttonequal:
                Calculater M = new Calculater();
                String mystring = all_looking.getText().toString();
                mystring = mystring + str;
                mystring = M.my_expression(mystring);
                all_looking.setText(mystring);
                str = "0";
                all_result.setText("0");
                clear_flag = true;
                clear_flag2 = false;
        }

    }

}

